//
//  GPLoadController.m
//  GoldPig
//
//  Created by 虎爷 on 2020/1/11.
//  Copyright © 2020 虎爷. All rights reserved.
//

#import "GPLoadController.h"
#import "GPHomeController.h"
#import "UUID.h"

@interface GPLoadController ()
@property (strong, nonatomic) UIWindow * window;
@end

@implementation GPLoadController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initData];
  
}

-(void)initData{
    [self initConfig];
    [self initOnline];
}

- (void)initConfig{
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"金猪生大钱",@"productName",@"0",@"channelId",VERSION,@"version", nil];
    [[GPRequsetManager HttpRequstManager] PostURL:Login_IP(switchConfig) params:dic2 success:^(NSInteger code, id  _Nonnull result) {
        //NSLog(@"%@",result);
        
        if (code == 1) {
            NSArray *data = result[@"data"];
            NSDictionary *dic = data[0];
            NSNumber *wx_switch = dic[[NSString stringWithFormat:@"wx_switch_0_%@",VERSION]];
            [JPAccount sharedJPAccount].wx_switch = wx_switch;
            [[JPAccount sharedJPAccount] SaveSwitchToSandbox];
        
            [self initWindow];
            
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}


-(void)initOnline{
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    // NSLog(@"之前时间：%@", [userDefault objectForKey:@"nowDate"]);//之前存储的时间
    // NSLog(@"现在时间%@",[NSDate date]);//现在的时间
    NSDate *now = [NSDate date];
    NSDate *agoDate = [userDefault objectForKey:@"nowDate"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *ageDateString = [dateFormatter stringFromDate:agoDate];
    NSString *nowDateString = [dateFormatter stringFromDate:now];
    // NSLog(@"日期比较：之前：%@ 现在：%@",ageDateString,nowDateString);
    if ( [ageDateString isEqualToString:nowDateString]) {
//        [self showErrorHubContent:@"每日仅能批量邀请一次"];
    }else{
        [[GPRequsetManager HttpRequstManager] PostURL2:Tree_IP(servicesOnline) params:[NSDictionary new]  success:^(NSInteger code, id  _Nonnull result) {

            if (code == 1) {
                //NSLog(@"%@",result);
                NSDate *nowDate = [NSDate date];
                NSUserDefaults *dataUser = [NSUserDefaults standardUserDefaults];
                [dataUser setObject:nowDate forKey:@"nowDate"];
                [dataUser synchronize];

            }

        } fail:^(NSError * _Nonnull error) {

        }];
    }

}

-(void)noAuthRequest{
    NSString * uuid= [UUID getUUID];
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:uuid,@"machineId",@"0",@"channelId",VERSION,@"version", nil];

    [[GPRequsetManager HttpRequstManager] PostURL:Login_IP(noAuth) params:dic  success:^(NSInteger code, id  _Nonnull result) {

        if (code == 1) {
            NSLog(@"%@",result);

            [JPAccount sharedJPAccount].token = result[@"data"][0][@"token"];
            [[JPAccount sharedJPAccount] SaveTokenToSandbox];

            [self initWindow];
        }

    } fail:^(NSError * _Nonnull error) {

    }];
}

- (void)initWindow{
    
    [[JPAccount sharedJPAccount]LoadTokenFromSandbox];
    NSString *token = [JPAccount sharedJPAccount].token;
    if (![token length]) {
        [self noAuthRequest];

    }else{
        
        [[GPRequsetManager HttpRequstManager] PostURL2:Tree_IP(getInfo) params:[NSDictionary new] success:^(NSInteger code, id  _Nonnull result) {
            //NSLog(@"%@",result);
            
            if (code == 1) {
                NSArray *data = result[@"data"];
                NSDictionary *dic = data[0];
                
                NSString *userName = dic[@"userName"];
                User *user = [[User alloc]init];
                user.headimgurl = dic[@"headImg"];
                user.nickname = userName;
                user.customKey = dic[@"customKey"];
                user.wxOpenId = dic[@"wxOpenId"];
                [JPUserTool saveUser:user];

                self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
                self.window.backgroundColor = [UIColor whiteColor];
                [self.window makeKeyAndVisible];

                GPHomeController *vc = [[GPHomeController alloc]init];
                UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
                self.window.rootViewController = nav;
              
            }
        } fail:^(NSError * _Nonnull error) {
            
        }];
    }
}





@end
